#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
"""
@version: 0.1
@author: 张宇洁
@contact: a_ff1984@163.com
@site: https://gitee.com/moluweichen
@software: PyCharm
@file: utils.py
@time: 2021/11/6 13:11
"""

import numpy as np
from configuration import configuration


def is_int(value):
    if not isinstance(value, (np.int64, np.int32, np.int16, np.int8, np.int0)):
        raise TypeError("argument expected int but got %s" % type(value).__name__)


def anliquates(pcs, weight, price, tax_rate):
    """
    获取整除数列表
    :param pcs:
    :param weight:
    :return:
    """

    is_int(pcs)
    is_int(weight)
    top_value = pcs // weight
    final_rest_list = list()
    while 1:
        if top_value <= 1:
            break
        if pcs % top_value == 0:
            final_rest_list.append(top_value)
        top_value -= 1
    if len(final_rest_list) == 0:
        return None
    return quantile(final_rest_list, pcs, price, tax_rate)


def quantile(anli_list, pcs, price, tax_rate):
    """
    取相应的分位整数
    :param anli_list:
    :return:
    """
    anli_list_length = len(anli_list)
    if anli_list_length == 1:
        return anli_list[0]
    elif anli_list_length > 1:
        pcs_num_real = None
        for pcs_num in anli_list:
            new_pcs = pcs // pcs_num
            if new_pcs * price * tax_rate < configuration.config.benchmark:
                pcs_num_real = pcs_num
                break
        if pcs_num_real is None:
            pos = float(configuration.config.pos)
            index = int((anli_list_length - 1) * pos)
            return anli_list[index]
        else:
            return pcs_num_real
    else:
        return anli_list[0]
